<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/icon.css"/>">
<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
</head>
<body>
<script>
var bigtitle;
var price;//单价
//获取新添加的手术id
function getnewid() {
	$.getJSON('Aopid', function(data) {
		$("#ssNo3").combogrid("setValue", data)
		
			$('#operation-apparatuslist').datagrid({
			url : 'allOperationApparatus',
			method : 'post',
			queryParams : {
				ssNo:data
			}
		})
		
		$('#operation-druglist').datagrid({
			url : 'AllOperationDrug',
			method : 'post',
			queryParams : {
				ssNo:data
			}
		})
	})
}
$(function(){
	//加载手术ID
	$("#ssNo3").combogrid({
		onOpen:function(){
			getnewid()
			bigtitle=$("#operation-list")
		}
	})
	$("#operation-ControllerTab").tabs({
		onSelect:function(title,index){
			if(title=='手术列表'){
				bigtitle=$("#operation-list")
			}else if(title== '手术药品管理'){
				bigtitle=$("#operation-druglist")
			}else{
				bigtitle=$("#operation-apparatuslist")
			}
		}
	})
	//提交手术列表数据
	$("#detail-save").linkbutton({
		onClick:function(){
			var rows=$("#operation-list").datagrid("getChanges")
			var aa=[];
				aa.push($("#ssNo3").combogrid("getValue"))
			for (var i = 0; i < rows.length; i++) {
				aa.push(rows[i].ssName)
				aa.push(rows[i].ssdotor)
				aa.push(rows[i].sssName)
				aa.push(rows[i].ssStarttime)
				aa.push(rows[i].ssEndtime)
				aa.push(rows[i].remarks)
			}
			$("<form>").form('submit',{
				url:'savesth',
				onSubmit: function(param){
			    	param.arr=aa
			    },
				success:function(data){
					var json = eval("(" + data + ")");
					$.messager.alert("提示", json.reason);
				}
			})
		}
	})
	//提交手术药品数据
	$("#ssdrug-save").linkbutton({
		onClick:function(){
			var rows=$("#operation-druglist").datagrid("getChanges")
			var aa=[];
			for (var i = 0; i < rows.length; i++) {
				aa.push(rows[i].ssypNo)
				aa.push(rows[i].cpCount)
				aa.push(rows[i].cpPrice)
			}
			$("<form>").form('submit',{
				url:'getUpOperationDrug',
				onSubmit: function(param){
			    	param.arr=aa
			    },
				success:function(data){
					var json = eval("(" + data + ")");
					$.messager.alert("提示", json.reason);
				}
			})
		}
	})
	var s=0;
	var index2;
	//行单击事件
	$("#operation-list").datagrid({
		onClickRow:function(index,data){
			$("#ssNo3").combogrid("setValue", data.ssNo)
			if(s!=0){
				var rows=$("#operation-list").datagrid("getSelections");
				if(index==index2){
					$("#operation-list").datagrid("unselectRow",index);
					s=0;
				}else{
					$("#operation-list").datagrid("unselectRow",index2);
					$("#operation-list").datagrid("selectRow",index);
					s=0
				}
			}
			else{
				index2=index;
				s++
			}
		}
	})
	
	//单价
	$("#operation-druglist").datagrid({
		onClickRow:function(index,data){
			price=data.cpPrice/data.cpCount
		}
	})
	//单价
	$("#operation-apparatuslist").datagrid({
		onClickRow:function(index,data){
			price=data.cpPrice/data.cpCount
		}
	})
	
	//显示对应的手术用品
	$("#ssNo3").combogrid({
		onChange:function(newValue,oldValue){
			ljshuju(newValue)
		}
	})
	
	//提交手术器械数据
	$("#apparatus-save").linkbutton({
		onClick:function(){
			var rows=$("#operation-apparatuslist").datagrid("getChanges")
			var aa=[];
			for (var i = 0; i < rows.length; i++) {
				aa.push(rows[i].ssqxNo)
				aa.push(rows[i].cpCount)
				aa.push(rows[i].cpPrice)
			}
			$("<form>").form('submit',{
				url:'getUpOperationapparatus',
				onSubmit: function(param){
			    	param.arr=aa
			    },
				success:function(data){
					var json = eval("(" + data + ")");
					$.messager.alert("提示", json.reason);
				}
			})
		}
	})
	//根据药品数量变更价格
	$("#operation-druglist").datagrid({
				onAfterEdit:function(rowIndex, rowData, changes){
					var as=rowData.cpCount;
					var c=price*as;
					$('#operation-druglist').datagrid('updateRow',{
						index: rowIndex,
						row: {
							cpPrice: c
						}
					});
				}
			})
	//根据器械数量变更价格
	$("#operation-apparatuslist").datagrid({
				onAfterEdit:function(rowIndex, rowData, changes){
					var as=rowData.cpCount;
					var c=price*as;
					$('#operation-apparatuslist').datagrid('updateRow',{
						index: rowIndex,
						row: {
							cpPrice: c
						}
					});
				}
			})
			
			//===============================
		$("#particulars").linkbutton({onClick:function(){
			var rows = $("#operation-list").datagrid("getSelections");
			var row = $("#ssNo3").combogrid("getValue");
			if(rows.length!=0){
					$("#operationparticulars").window({
						'title':'手术详情',
						'href':'to_nurse?ssNo='+row							
					})
					
					$("#operationparticulars").window("open");
					
				}else{
					$.messager.confirm("提示", "请选择要查看的手术！")
				}
		}})
})
//数据关联
function ljshuju(newValue){
	
	$('#operation-apparatuslist').datagrid({
	url : 'allOperationApparatus',
	method : 'post',
	queryParams : {
		ssNo:newValue
	}
})

$('#operation-druglist').datagrid({
	url : 'AllOperationDrug',
	method : 'post',
	queryParams : {
		ssNo:newValue
	}
})
}
$.extend($.fn.datagrid.methods, {
			editCell : function(jq, param) {
				return jq.each(function() {
					var opts = $(this).datagrid('options');
					var fields = $(this).datagrid('getColumnFields', true)
							.concat($(this).datagrid('getColumnFields'));
					for (var i = 0; i < fields.length; i++) {
						var col = $(this)
								.datagrid('getColumnOption', fields[i]);
						col.editor1 = col.editor;
						if (fields[i] != param.field) {
							col.editor = null;
						}
					}
					$(this).datagrid('beginEdit', param.index);
					for (var i = 0; i < fields.length; i++) {
						var col = $(this)
								.datagrid('getColumnOption', fields[i]);
						col.editor = col.editor1;
					}
				});
			}
		});

		var editIndex = undefined;
		function endEditing() {
			if (editIndex == undefined) {
				return true
			}
			if (bigtitle.datagrid('validateRow', editIndex)) {
				bigtitle.datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}
		function onClickCell(index, field) {
			if (endEditing()) {
				bigtitle.datagrid('selectRow', index).datagrid(
						'editCell', {
							index : index,
							field : field
						});
				editIndex = index;
			}
		}
		function reset(){
			bigtitle.datagrid("reload")
		}
		function detailremove(){
			var rows = $("#operation-list").datagrid("getSelections");
			if(rows.length==1){
			$.messager.confirm("提示", "确定要删除吗？", function(v) {
				if (v) {
					//用form提交
					$('<form>').form('submit', {
						url : 'deleteOperationDetail',
						onSubmit : function(param) {
							param.ssNo = rows[0].ssNo;
						},
						success : function(data) {
							var json = eval("(" + data + ")");
							$.messager.alert("提示", json.reason);
							$("#operation-list").datagrid("reload")
						}
					});
				}
			});
			}else{
				$.messager.confirm("提示", "您未选择行!")
			}
		}
		function drugremove(){
			var rows = $("#operation-druglist").datagrid("getSelections");
			if(rows.length!=0){
			$.messager.confirm("提示", "确定要删除吗？", function(v) {
				if (v) {
					//将编号保存到数组中
					var arr = [];
					for (var i = 0; i < rows.length; i++) {
						arr.push(rows[i].ssypNo);
					}
					//用form提交
					$('<form>').form('submit', {
						url : 'deleteOperationDrug',
						onSubmit : function(param) {
							param.list = arr;
						},
						success : function(data) {
							var json = eval("(" + data + ")");
							$.messager.alert("提示", json.reason);
							$("#operation-druglist").datagrid("reload")
						}
					});
				}
			});
			}else{
				$.messager.confirm("提示", "请选择要删除的药品！")
			}
		}
		function apparatusremove(){
			var rows = $("#operation-apparatuslist").datagrid("getSelections");
			if(rows.length!=0){
				$.messager.confirm("提示", "确定要删除吗？", function(v) {
					if (v) {
						//将编号保存到数组中
						var arr = [];
						for (var i = 0; i < rows.length; i++) {
							arr.push(rows[i].ssqxNo);
						}
						//用form提交
						$('<form>').form('submit', {
							url : 'deleteOperationapparatus',
							onSubmit : function(param) {
								param.list = arr;
							},
							success : function(data) {
								var json = eval("(" + data + ")");
								$.messager.alert("提示", json.reason);
								$("#operation-apparatuslist").datagrid("reload")
							}
						});
					}
				});
				}else{
					$.messager.confirm("提示", "请选择要删除的器械！")
				}
		}
</script>
	
		
			<div style="margin:10px">
				<select id="ssNo3" class="easyui-combogrid" name="ssNo"
					data-options="label:'手术名称',
																							width:'50%', 
																							editable:false, 
																							labelWidth:'25%',
																							panelWidth: '20%',
																							url:'opdrug',
																							panelHeight:'auto',
																							idField: 'ssNo',
																							textField: 'ssName',
																							columns:[[
																								{field:'ylkNo',title:'医疗卡号',width:95},
																				                {field:'brName',title:'病人姓名',width:95},
																				                {field:'ssName',title:'手术名称',width:95},
																				                {field:'brSex',title:'性别',width:95},
																				                {field:'brAge',title:'年龄',width:95},
																				                {field:'ksName',title:'科室',width:95}
																				            ]]
																							"></select>
			</div>
			<div class="easyui-tabs" id="operation-ControllerTab" style="width:100%;height:360px">
				
				<div title="手术列表" data-options="iconCls:'icon-operation'" style="padding:10px">
					<table id="operation-list" class="easyui-datagrid" data-options="width:'100%',
																	fitColumns:true,fit:true,
																	pagination: true,
																	toolbar:'#ss-listtoolbar',
																	url:'AllDetailOP',
																	singleSelect:true,
																	pageSize: 5,
																	pageList: [5, 10, 15, 20],
																	onDblClickCell:onClickCell,
																	rownumbers: true,
																    border: false
																	">
					<thead>
						<tr>
						<th data-options="field:'ssNo',width:'20%'">手术编号</th>
						<th data-options="field:'ssName',width:'20%',editor:'textbox'">手术名称</th>
								<th data-options="field:'ssdotor',width:'11%',editor:{
													type:'combobox',
													options:{
															editable:false, 
															url:'getdoctor',
															panelWidth: '5%',
															panelHeight:'100px',
															valueField: 'ygName',
															textField: 'ygName'
													}}">手术医生</th>
								<th data-options="field:'sssName',width:'12%',editor:{
															type:'combobox',
															options:{
															panelWidth: '7%',
															url:'Alloperationroom',
															panelHeight:'auto',
															valueField: 'sssName',
															textField: 'sssName'
															}
													}">手术室</th>
								<th data-options="field:'ssStarttime',width:'12%',editor:'datebox'">手术时间</th>
								<th data-options="field:'ssEndtime',width:'12%',editor:'datebox'">手术结束时间</th>
								<th data-options="field:'remarks',width:'15%',editor:'textbox'">备注</th>
								
						</tr>
					</thead>
				</table>
				
				<div id="ss-listtoolbar">
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" id="detail-save">保存</a>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true" onclick="detailremove()">删除</a>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reset()">撤销</a>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true" onclick="endEditing()">应用</a>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-particular',plain:true" id="particulars">详情</a>
				</div>
			</div>
				<div title="手术药品管理" data-options="iconCls:'icon-drug'" style="padding:10px">
					<table id="operation-druglist" class="easyui-datagrid" data-options="width:'100%',
																	fitColumns:true,fit:true,
																	pagination: true,
																	toolbar:'#drug-listtoolbar',
																	onDblClickCell:onClickCell,
																	pageSize: 5,
																	pageList: [5, 10, 15, 20],
																	rownumbers: true,
																    border: false
																	">
					<thead>
						<tr>
								<th data-options="field:'ssypNo',width:'23%'">药品编号</th>
								<th data-options="field:'cpName',width:'27%'">药品名称</th>
								<th data-options="field:'cpType',width:'15%'">类型</th>
								<th data-options="field:'cpUnit',width:'15%'">单位</th>
								<th data-options="field:'cpCount',width:'10%',editor:{type:'numberbox',options:{min:1}}">数量</th>
								<th data-options="field:'cpPrice',width:'13%'">价格</th>
								
						</tr>
					</thead>
				</table>
					<div id="drug-listtoolbar">
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" id="ssdrug-save">保存</a>
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true" onclick="drugremove()">删除</a>
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reset()">撤销</a>
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true" onclick="endEditing()">应用</a>
					</div>
				</div>
				<div title="手术器械管理" data-options="iconCls:'icon-apparatus'" style="padding:10px">
					<table id="operation-apparatuslist" class="easyui-datagrid" data-options="width:'100%',
																	fitColumns:true,fit:true,
																	pagination: true,
																	toolbar:'#apparatus-listtoolbar',
																	onDblClickCell:onClickCell,
																	pageSize: 5,
																	pageList: [5, 10, 15, 20],
																	rownumbers: true,
																    border: false
																	">
					<thead>
						<tr>
								<th data-options="field:'ssqxNo',width:'24%'">器械编号</th>
								<th data-options="field:'cpName',width:'30%'">器械名称</th>
								<th data-options="field:'cpCount',width:'15%',editor:{type:'numberbox',options:{min:1}}">数量</th>
								<th data-options="field:'cpPrice',width:'15%'">价格</th>
								<th data-options="field:'qxPh',width:'18%'">批号</th>
								
						</tr>
					</thead>
				</table>
					<div id="apparatus-listtoolbar">
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" id="apparatus-save">保存</a>
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true" onclick="apparatusremove()">删除</a>
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reset()">撤销</a>
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true" onclick="endEditing()">应用</a>
					</div>
				</div>
			</div>
			<div id="operationparticulars" class="easyui-window" title="手术详情" data-options="collapsible:false,minimizable:false,maximizable:false,closed:true" 
				style="width:30%; height:400px"></div>	
</body>
</html>